package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Logistics;
import com.longdaji.decoration.bean.MyLogistics;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Kanade on 2018/4/30 0030.
 */

public class SeeLogisticsAdapter extends RecyclerView.Adapter<SeeLogisticsAdapter.LogisticsHolder> {
    private List<MyLogistics.TracesBean> mLogisticsList;
    private Context mContext;

    public SeeLogisticsAdapter(List<MyLogistics.TracesBean> logisticsList) {
        this.mLogisticsList = getProgressedList(logisticsList);
    }

    class LogisticsHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_logistics_date)
        TextView tvLogisticsDate;
        @BindView(R.id.tv_logistics_time)
        TextView tvLogisticsTime;
        @BindView(R.id.iv_logistics_status)
        ImageView ivLogisticsStatus;
        @BindView(R.id.tv_logistics_status)
        TextView tvLogisticsStatus;
        @BindView(R.id.tv_logistics_information)
        TextView tvLogisticsInformation;
        @BindView(R.id.view_process_line)
        View vProcessLine;

        public LogisticsHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public LogisticsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_see_logistics, parent, false);
        LogisticsHolder holder = new LogisticsHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(LogisticsHolder holder, int position) {
        MyLogistics.TracesBean logistics = mLogisticsList.get(position);
        if (position == mLogisticsList.size() - 1) {
            holder.vProcessLine.setVisibility(View.INVISIBLE);
        } else if (position == 0) {
            holder.vProcessLine.setBackgroundColor(mContext.getResources().getColor(R.color.text_color_type_four));
            holder.tvLogisticsDate.setTextColor(mContext.getResources().getColor(R.color.text_color_type_four));
            holder.tvLogisticsTime.setTextColor(mContext.getResources().getColor(R.color.text_color_type_four));
            holder.tvLogisticsStatus.setTextColor(mContext.getResources().getColor(R.color.text_color_type_four));
            holder.tvLogisticsInformation.setTextColor(mContext.getResources().getColor(R.color.text_color_type_four));
            Glide.with(mContext).load(R.mipmap.sign_receive).into(holder.ivLogisticsStatus);
            holder.vProcessLine.setVisibility(View.VISIBLE);
        } else {
            // TODO: 2018/3/13 一般item
            holder.vProcessLine.setVisibility(View.VISIBLE);
        }
        String[] dateAndTime = StringUtil.splitTime(logistics.getAcceptTime());
        LogUtil.d("mytest", "date: " + dateAndTime[0] + "time: " + dateAndTime[1]);
        holder.tvLogisticsDate.setText(dateAndTime[0]);
        holder.tvLogisticsTime.setText(dateAndTime[1]);
//        holder.tvLogisticsStatus.setText(logistics.getAcceptStation());
        holder.tvLogisticsInformation.setText(logistics.getAcceptStation());

    }

    @Override
    public int getItemCount() {
        return mLogisticsList.size();
    }

    //反序
    private List<MyLogistics.TracesBean> getProgressedList(List<MyLogistics.TracesBean> list) {
        List<MyLogistics.TracesBean> tmpList = new ArrayList<>();
        for (int i = list.size() - 1; i >= 0; i--) {
            tmpList.add(list.get(i));
        }
        return tmpList;
    }

    public void setData(List<MyLogistics.TracesBean> list) {
        mLogisticsList.clear();
        mLogisticsList.addAll(getProgressedList(list));
        notifyDataSetChanged();
    }


}